package com.example.demo.study.thread.threadLocalTry.yes;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Case1DateParse {
    // 1. 创建ThreadLocal，每个线程独立初始化SimpleDateFormat
    public static ThreadLocal<SimpleDateFormat> sdfLocal=ThreadLocal.withInitial(()->
            new SimpleDateFormat("yyyy-MM-dd")
            );
    public static void main(String[] args) throws ParseException {
        for(int i=0;i<10;i++)
        {
            new Thread(()->{
                // 2. 每个线程从自己的ThreadLocal获取实例
                SimpleDateFormat sf=sdfLocal.get();
                try {
                    System.out.println(sf.parse("2024-07-24"));
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }).start();
        }
    }
}
